﻿using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Security.Principal;

namespace MyFramework.Helpers
{
    /// <summary>
    /// Exception helper
    /// </summary>
    public static class ExceptionHelper
    {
        /// <summary>
        /// parent assembly
        /// </summary>
        private static Assembly parentAssembly;

        /// <summary>
        /// get IP address of this machine
        /// not an ideal method for a number of reasons (guess why!)
        /// but the alternatives are very ugly
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentIP()
        {
            try
            {
                var query = from ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList where ip.AddressFamily != AddressFamily.InterNetworkV6 select ip;
                return query.FirstOrDefault().ToString();
            }
            catch (Exception)
            {
                return "127.0.0.1";
            }
        }

        /// <summary>
        /// Gets the parent assembly.
        /// </summary>
        /// <returns></returns>
        public static Assembly GetParentAssembly()
        {
            if (parentAssembly == null)
            {
                if (System.Reflection.Assembly.GetEntryAssembly() == null)
                {
                    parentAssembly = System.Reflection.Assembly.GetCallingAssembly();
                }
                else
                {
                    parentAssembly = System.Reflection.Assembly.GetEntryAssembly();
                }
            }

            return parentAssembly;
        }

        /// <summary>
        /// Retrieve identity with fallback on error to safer method
        /// </summary>
        /// <returns></returns>
        public static string GetUserIdentity()
        {
            string userIdentity = null;
            userIdentity = CurrentWindowsIdentity();
            if (string.IsNullOrEmpty(userIdentity))
            {
                userIdentity = CurrentEnvironmentIdentity();
            }

            return userIdentity;
        }


        /// <summary>
        /// Exception-safe WindowsIdentity.GetCurrent retrieval returns "domain\username"
        /// per MS, this sometimes randomly fails with "Access Denied" particularly on NT4
        /// </summary>
        /// <returns></returns>
        public static string CurrentWindowsIdentity()
        {
            try
            {
                return WindowsIdentity.GetCurrent().Name;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Exception-safe "domain\username" retrieval from Environment
        /// </summary>
        /// <returns></returns>
        public static string CurrentEnvironmentIdentity()
        {
            try
            {
                return Environment.UserDomainName + "\\" + Environment.UserName;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }
}
